package com.le.util;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;

/**
 * Author : le
 * Version : 1.0
 * 用法示例
 * public static void main(String[] args) {
 * Query query = new Query("10.69.3.234");
 * query.setDatabaseName("test2021");
 * query.setSQL("select student.SNO, student.Sname, course.cname, sc.grade from student, sc, course\n" +
 * "where student.SNO = 201215122 and student.SNO = sc.sno  and sc.cno = course.cno");
 * String[][] recorde = query.getRecord();
 * DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
 * for (int i = 0; i < recorde.length; i++) {
 * dataset1.addValue(Double.parseDouble(recorde[i][3]), recorde[i][1], recorde[i][2]);
 * }
 * JPanel bar = plotBar(dataset1, recorde[0][1] + "各科成绩分布图", "科目", "成绩");
 * JFrame jFrame = new JFrame("成绩图绘制");
 * jFrame.add(bar);
 * jFrame.setBounds(0, 0, 1080, 720);
 * jFrame.setVisible(true);
 * }
 */

public class Plot {
    public static JPanel plotBar(DefaultCategoryDataset dataset, String title, String categoryAxisLabel, String valueAxisLabel) {
        JFreeChart chart = ChartFactory.createBarChart3D(title, categoryAxisLabel,
                valueAxisLabel,
                dataset,
                PlotOrientation.VERTICAL,
                true,
                false,
                false);
        CategoryPlot plot = chart.getCategoryPlot();

        CategoryAxis domain = plot.getDomainAxis();
        domain.setTickLabelFont(new Font("黑体", Font.BOLD, 12));
        domain.setLabelFont(new Font("黑体", Font.BOLD, 16));

        ValueAxis rangeAxis = plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 12));

        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 16));
        chart.getTitle().setFont(new Font("黑体", Font.BOLD, 16));

        return new ChartPanel(chart);
    }
}
